package clientTest;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class RoutingTest {

    RestHighLevelClient client;

    @Before
    public void test0() {
        client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("192.168.153.131", 9200, "http")));
    }

    @Test
    public void init_routing() throws IOException {
        Map<String, Object> jsonMap1 = new HashMap<>();
        jsonMap1.put("phone", "13612312312");
        jsonMap1.put("name", "a");
        jsonMap1.put("sex", "m");
        jsonMap1.put("age", "1");

        Map<String, Object> jsonMap2 = new HashMap<>();
        jsonMap2.put("phone", "18948923942");
        jsonMap2.put("name", "b");
        jsonMap2.put("sex", "w");
        jsonMap2.put("age", "2");

        Map<String, Object> jsonMap3 = new HashMap<>();
        jsonMap3.put("phone", "1528934829432");
        jsonMap3.put("name", "c");
        jsonMap3.put("sex", "m");
        jsonMap3.put("age", "3");

        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest("test8", "user")
                .routing(jsonMap1.get("phone").toString().substring(0, 3))
                .source(jsonMap1));
        request.add(new IndexRequest("test8", "user")
                .routing(jsonMap2.get("phone").toString().substring(0, 3))
                .source(jsonMap2));
        request.add(new IndexRequest("test8", "user")
                .routing(jsonMap3.get("phone").toString().substring(0, 3))
                .source(jsonMap3));

        BulkResponse bulkItemResponses = client.bulk(request, RequestOptions.DEFAULT);
        if (bulkItemResponses.hasFailures()) {
            for (BulkItemResponse bulkItemResponse : bulkItemResponses) {
                if (bulkItemResponse.isFailed()) {
                    BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
                    System.out.println(failure.getMessage());
                }
            }
        }

        client.close();
    }

    @Test
    public void test_routing() throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery())
                .from(0)
                .size(10);

        searchRequest.indices("test8");
        searchRequest.types("user");
        searchRequest.source(searchSourceBuilder);
        searchRequest.routing("13612312312".substring(0, 3));

        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        for (SearchHit hit : hits.getHits()) {
            System.out.println(hit);
        }

        client.close();
    }
}
